using System;
using System.Collections.Generic;

namespace DictionaryExample // 命名空间
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 创建一个 Dictionary
            Dictionary<int, string> studentRecords = new Dictionary<int, string>();

            // 添加键值对到 Dictionary
            studentRecords.Add(1, "Alice");
            studentRecords.Add(2, "Bob");
            studentRecords.Add(3, "Charlie");

            // 打印所有学生记录
            Console.WriteLine("Student Records:");
            foreach (var record in studentRecords)
            {
                Console.WriteLine($"ID: {record.Key}, Name: {record.Value}");
            }

            // 更新一个学生记录
            studentRecords[2] = "David"; // 将 ID 为 2 的名字更新为 "David"

            Console.WriteLine("\nAfter updating a student record:");
            foreach (var record in studentRecords)
            {
                Console.WriteLine($"ID: {record.Key}, Name: {record.Value}");
            }

            // 删除一个学生记录
            studentRecords.Remove(3); // 删除 ID 为 3 的记录

            Console.WriteLine("\nAfter removing a student record:");
            foreach (var record in studentRecords)
            {
                Console.WriteLine($"ID: {record.Key}, Name: {record.Value}");
            }

            // 尝试访问一个不存在的键
            if (studentRecords.TryGetValue(5, out string name))
            {
                Console.WriteLine($"\nStudent with ID 5: {name}");
            }
            else
            {
                Console.WriteLine("\nStudent with ID 5 does not exist.");
            }

            // 输出字典的总数
            Console.WriteLine($"\nTotal student records: {studentRecords.Count}");
        }
    }
}
